package com.movitech.mobile.cz.base.sys.controller;


import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.sys.bizvo.ProjectRequestVO;
import com.movitech.mobile.cz.base.sys.entity.SProject;
import com.movitech.mobile.cz.base.sys.service.SProjectService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
 * <p>
 * SProject 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/sProject")
@Api(value = "项目信息基础操作接口", description = "项目信息基础操作接口")
public class SProjectController {
    @Autowired
    @Qualifier("sProjectService")
    private SProjectService sProjectService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有项目信息接口",
            notes = "获取所有项目信息接口",
            response = SProject.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SProject> page = sProjectService.selectPage(new Page<SProject>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "项目信息列表查询接口",
            notes = "项目信息列表查询接口",
            response = SProject.class
    )
    public RestReturnResult searchList(@RequestBody SProject sProject,
                @ApiParam(value = "分页页数")@PathVariable Integer pageNumber) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SProject> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sProject);
        page = sProjectService.selectPage(page,wrapper);
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @GetMapping("/getSProject/{id}")
    @ApiOperation(
            value = "项目信息对象ID获取接口",
            notes = "项目信息对对象ID获取接口",
            response = SProject.class
    )
    public RestReturnResult addSProject(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        SProject sProject = sProjectService.selectById(id);
        restReturnResult.setData(sProject);
        return restReturnResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "项目信息添加接口",
            notes = "项目信息添加接口",
            response = SProject.class
    )
    public RestReturnResult add(@RequestBody SProject sProject) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sProjectService.insert(sProject);
        restReturnResult.setData(sProject);
        return restReturnResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "项目信息更新接口",
            notes = "项目信息更新接口",
            response = SProject.class
    )
    public RestReturnResult update(@RequestBody SProject sProject) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sProjectService.updateById(sProject);
        restReturnResult.setData(sProject);
        return restReturnResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "项目信息通过ID删除接口",
            notes = "项目信息通过ID删除接口",
            response = SProject.class
    )
    public RestReturnResult update(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sProjectService.deleteById(id);
        return restReturnResult;
    }

    /**
	 * 项目列表接口
	 * @param 
	 * @return
	 */
    @ApiOperation(
            value = "项目列表接口",
            notes = "项目列表接口",
            response = SProject.class
    )
	@RequestMapping(value = "/getProjectListByRegionalCompanyId", method = RequestMethod.POST)
	@ResponseBody
	public RestReturnResult<Object> getProjectListByRegionalCompanyId(@RequestBody ProjectRequestVO projectRequestVO){
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

		try{
			if(projectRequestVO != null){
				if(projectRequestVO.getPageFlag() == 1){//分页
					Page<SProject> page = new Page<>();
			        page.setCurrent(projectRequestVO.getPageNumber());

			        SProject sProject = new SProject();
			        sProject.setRegionalCompanyId(projectRequestVO.getRegionalCompanyId());
			        sProject.setDelFlag(CommonConstants.NO_INTEGER);
			        EntityWrapper wrapper = new EntityWrapper<>();
			        wrapper.setEntity(sProject);
			        page = sProjectService.selectPage(page,wrapper);

			        hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, page);
				}else{
					SProject sProject = new SProject();
					sProject.setRegionalCompanyId(projectRequestVO.getRegionalCompanyId());
					sProject.setDelFlag(CommonConstants.NO_INTEGER);
					EntityWrapper wrapper = new EntityWrapper<>();
					wrapper.setEntity(sProject);
					List<SProject> result = sProjectService.selectList(wrapper);
					
					hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, result);
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
		}

		return hResult;
	}

}

